package com.buu.education.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.buu.common.annotation.Log;
import com.buu.common.utils.JSONUtils;
import com.buu.common.utils.PageUtils;
import com.buu.common.utils.Query;
import com.buu.common.utils.R;
import com.buu.education.domain.EducationEvaluateDO;
import com.buu.education.service.EvaluateService;
import com.buu.manager.domain.EduDO;
import com.buu.manager.service.EduService;
import com.buu.system.domain.SysUserDO;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 教学评价，包括评价老师和学生
 */
@RequestMapping("/education_evaluate")
@Controller
public class EducationEvaluateController {

    @Autowired
    private EduService eduService;

    @Autowired
    private EvaluateService evaluateService;

    /**
     * 返回评价学生页面
     * @param model
     * @return
     */
    @GetMapping("/student")
    String student(Model model) {
        Subject subject = SecurityUtils.getSubject();
        SysUserDO sysUserDO = (SysUserDO) subject.getPrincipal();
        return "education/evaluate/student";
    }

    /**
     * 返回评价教师页面
     * @param model
     * @return
     */
    @GetMapping("/teacher")
    String teacher(Model model) {
        Subject subject = SecurityUtils.getSubject();
        SysUserDO sysUserDO = (SysUserDO) subject.getPrincipal();
        return "education/evaluate/teacher";
    }

    /**
     * 获取学生用户信息
     * @param params
     * @return
     */
    @GetMapping("/list_stu")
    @ResponseBody
    PageUtils listStu(@RequestParam Map<String, Object> params) {
        //获取当前登录用户
        Subject subject = SecurityUtils.getSubject();
        SysUserDO sysUserDO = (SysUserDO) subject.getPrincipal();
        //根据用户获取身份
        EduDO eduDO = eduService.getUserBySysUserId(sysUserDO.getUserId());
        if(eduDO != null){
            //获取学生所在年级，如果是教师，获取所有教师
            if("2".equals(params.get("type"))){
                params.put("unit", eduDO.getUnit());
            }
            //不评价自己
//            params.put("userId", sysUserDO.getUserId());
            //已评价的不显示
            params.put("evaUserId", eduDO.getId());
            // 查询列表数据
            Query query = new Query(params);
            List<EducationEvaluateDO> edus = evaluateService.listByTeacher(query);
            int total = evaluateService.listByTeacherCount(query);
            PageUtils pageUtil = new PageUtils(edus, total);
            return pageUtil;
        }
        return null;
    }

    /**
     * 获取需评价的教师信息
     * @param params
     * @return
     */
    @GetMapping("/list_tea")
    @ResponseBody
    PageUtils listTea(@RequestParam Map<String, Object> params) {
        //获取当前登录用户
        Subject subject = SecurityUtils.getSubject();
        SysUserDO sysUserDO = (SysUserDO) subject.getPrincipal();
        //根据用户获取身份
        EduDO eduDO = eduService.getUserBySysUserId(sysUserDO.getUserId());
        if(eduDO != null){
            //年级，后期如果想获取所有老师，unit不传即可
            params.put("unit", eduDO.getUnit());
            //不评价自己
            params.put("userId", sysUserDO.getUserId());
            //已评价的不显示
            params.put("evaUserId", eduDO.getId());
            // 查询列表数据
            Query query = new Query(params);
            List<EducationEvaluateDO> edus = evaluateService.listByUnit(query);
            int total = evaluateService.listByUnitCount(query);
            PageUtils pageUtil = new PageUtils(edus, total);
            return pageUtil;
        }
        return null;
    }


    @Log("开始评价")
    @GetMapping("/eva/{eduUserId}")
    String edit(Model model, @PathVariable("eduUserId") String eduUserId) {
//        EduDO eduDO = eduService.get(id);
//        model.addAttribute("user", eduDO);
        //获取评价对象信息
        EduDO eduDO = eduService.getUserBySysUserId(Long.valueOf(eduUserId));
        switch (eduDO.getUnit()){
            case "1":
                eduDO.setUnit("一年级");
                break;
            case "2":
                eduDO.setUnit("二年级");
                break;
            case "3":
                eduDO.setUnit("三年级");
                break;
            case "4":
                eduDO.setUnit("四年级");
                break;
            case "5":
                eduDO.setUnit("五年级");
                break;
            case "6":
                eduDO.setUnit("六年级");
                break;
        }
        model.addAttribute("user", eduDO);
        return "education/evaluate/evaluate";
    }

    @Log("保存评价")
    @ResponseBody
    @PostMapping("/save")
    R save(@RequestParam Map<String, Object> params) {
        List<Map> list = JSON.parseArray(String.valueOf(params.get("arrayObj")), Map.class);
        //选项
        List<String> choose = new ArrayList<>();
        //分数
        List<String> goals = new ArrayList<>();
        int goal = 0;
        for(Map map : list){
            //获取选项
            choose.add(String.valueOf((Integer) map.get("index")));
            //获取得分
            String goalsStr = (String) map.get("value");
            goals.add(goalsStr);
            goal = goal + Integer.parseInt(goalsStr);
        }
        //用户id
        String userId = (String) params.get("id");
        //对象类型，1-教师，2-学生
        String type = (String) params.get("type");
        //对象名称
        String name = (String) params.get("name");
        //评语
        String remark = (String) params.get("mark");
        EducationEvaluateDO educationEvaluateDO = new EducationEvaluateDO();
        educationEvaluateDO.setUserId(userId);
        educationEvaluateDO.setType(type);
        educationEvaluateDO.setGoal(String.valueOf(goal));
        educationEvaluateDO.setChoose(String.join(",", choose));
        educationEvaluateDO.setGoals(String.join(",", goals));
        educationEvaluateDO.setRemark(remark);
        educationEvaluateDO.setName(name);
        //获取当前登录用户
        Subject subject = SecurityUtils.getSubject();
        SysUserDO sysUserDO = (SysUserDO) subject.getPrincipal();
        //根据用户获取身份
        EduDO eduDO = eduService.getUserBySysUserId(sysUserDO.getUserId());
        educationEvaluateDO.setEvaUserId(eduDO.getId());
        educationEvaluateDO.setEvaName(eduDO.getName());

        int insertResult = evaluateService.save(educationEvaluateDO);
        if(insertResult > 0){
            return R.ok();
        }

        return R.error();
    }

    @Log("评价查看")
    @GetMapping("/view/{id}")
    String view(Model model, @PathVariable("id") Long id) {
//        EduDO eduDO = eduService.get(id);
//        model.addAttribute("user", eduDO);
        //获取评价对象信息
        EducationEvaluateDO educationEvaluateDO = evaluateService.get(id);
        switch (educationEvaluateDO.getUnit()){
            case "1":
                educationEvaluateDO.setUnit("一年级");
                break;
            case "2":
                educationEvaluateDO.setUnit("二年级");
                break;
            case "3":
                educationEvaluateDO.setUnit("三年级");
                break;
            case "4":
                educationEvaluateDO.setUnit("四年级");
                break;
            case "5":
                educationEvaluateDO.setUnit("五年级");
                break;
            case "6":
                educationEvaluateDO.setUnit("六年级");
                break;
        }
        model.addAttribute("user", educationEvaluateDO);
        return "education/evaluate/view";
    }
}
